package org.example.file.serialize;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TestObj {

    public static void main(String[] args) throws Exception {

        List<Person> list = new ArrayList<>();
        int num = 100;
        for (int i = 0; i < num; i++) {
            Person person = new Person();
            person.setName(i + " ");
            person.setAge(i);
            if (i % 2 == 0) {
                person.setClothes(true);
            } else {
                person.setClothes(false);
            }

            person.setMoney(new byte[] {1, 2, 3});

            list.add(person);
        }

        File file = new File("/Users/zhangjungang/test/person");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream otm = new ObjectOutputStream(fos);
        for (Person p : list) {
            otm.writeObject(p);
            // otm.flush();
        }
        otm.writeObject(null);
        otm.flush();
        otm.close();
        fos.close();
        System.out.println("-------object write end!");

        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);

        while (true) {
            Object obj = ois.readObject();
            if (obj != null) {
                Person p = (Person)ois.readObject();
                System.out.println(p);
            } else {
                break;
            }
        }

        ois.close();
        fis.close();
        System.out.println("-------object read end!");

    }

}
